﻿// ***************************************************************************
// YAMM
// Copyright (C) 2008 Christian Schmiljun, Frank Weißenborn 
// http://www.codeplex.com/YAMM 
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// ***************************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace YAMM
{    
    /// <summary>
    /// Control to show informations about a Plugin.
    /// </summary>
    internal partial class PluginInfo : UserControl
    {
        private YAMM.PlugIn.IPlugin plugin;

        /// <summary>
        /// Occurs when user will delete a plugin.
        /// </summary>
        public event EventHandler DeletePlugin;
        
        /// <summary>
        /// Gets the GUID of the plugin.
        /// </summary>
        /// <value>The GUID.</value>
        public string GUID
        {
            get { return plugin.Guid.ToString(); }            
        }

        /// <summary>
        /// Flag, if the plugin is marked as deleted
        /// </summary>
        private bool flagDeleted = false;

        public bool FlagDeleted
        {
            get { return flagDeleted; }
            set 
            { 
                flagDeleted = value;
                UpdateGUI();
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PluginInfo"/> class.
        /// </summary>
        private PluginInfo()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PluginInfo"/> class.
        /// </summary>
        /// <param name="plugin">The plugin.</param>
        public PluginInfo(YAMM.PlugIn.IPlugin plugin)
            : this()
        {
            this.plugin = plugin;            
            UpdateGUI();
        }

        private void UpdateGUI()
        {
            // update text 
            richTextBox1.Rtf = @"{\rtf1\ansi \b " + plugin.Name + @" \b0 \par\par"
                + @"\b Author:\b0\tab " + plugin.Author + @"\tab\tab \b Version:\b0\tab " + plugin.Version + @"\par"
                + @"\b Beschreibung:\b0\tab " + plugin.Description;
            PluginInfo_Resize(this, EventArgs.Empty);
            // update icon
            pictureBox1.Image = plugin.Icon;
            
            //config button
            configPluginButton.Visible = plugin.IsConfigureable();

            //dont show the delete message
            labelMessage.Visible = false;

            if (flagDeleted)
            {
                configPluginButton.Visible = false;
                deletePluginButton.Visible = false;
                labelMessage.Visible = true;
             
            }
            pictureBox1.Focus();
        }

        private void PluginInfo_Resize(object sender, EventArgs e)
        {
            // TODO benötigte Größe ggf. besser berechnen
            float a = richTextBox1.Font.SizeInPoints * plugin.Description.Length * 0.7f;
            int t = Convert.ToInt32(a / richTextBox1.Width) * 20 + 80;
            if (t > this.MinimumSize.Height)
                this.Height = t;
            else
                this.Height = MinimumSize.Height;
        }

        private void ConfigPluginButton_Click(object sender, EventArgs e)
        {
            plugin.Configure();
        }

        private void DeletePluginButton_Click(object sender, EventArgs e)
        {
            if (DeletePlugin != null)
                DeletePlugin(this,null);            
        }        

    }
}
